<?php
/**
 * Happy 精迅CMS
 * =======================================================
 * 版权所有 (C) 2010-2020 www.gift400.com，并保留所有权利。
 * 网站地址: http://www.gift400.com
 * -------------------------------------------------------
 *
 * @author :     大成 <admin@gift400.com>
 * @version :    v1.x
 * =======================================================
 */
if (file_exists('./install_lock.php')) {
	echo "<script language=\"javascript\">alert(\"您已经安装了本系统，install.php 文件仍然在您的服务器上，请马上利用 FTP 来将其删除！\")</script>";
	exit;
}

error_reporting(7);
set_magic_quotes_runtime(0);
define('IN_Happy', true);
define('Happy_ADMIN', true);
define('Happy_ROOT', str_replace("\\", '/', dirname(__FILE__)) . '/');

if (!get_magic_quotes_gpc()) {
	$_GET = array_map("addslashes", $_GET);
	$_POST = array_map("addslashes", $_POST);
}

$action = ($_POST['action']) ? $_POST['action'] : $_GET['action'];
$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];

if (function_exists('set_time_limit') == 1 && @ini_get('safe_mode') == 0) {
	@set_time_limit(1000);
}



?>
<html>
<head>
<title>欢迎安装本系统</title>
<style type="text/css">
a { text-decoration: none;}
a:hover{ text-decoration: underline;}
.t {font-family: Verdana, Arial, Sans-serif;font-size  : 12px;padding-left: 10px;font-weight: normal;color : #333366;}
.r {font-family: Arial, Sans-serif;font-size  : 12px;font-weight: normal;line-height: 200%;color : #0000EE;}
.c {font-family: Arial, Sans-serif;font-size  : 12px;font-weight: normal;line-height: 200%;color : #EE0000;}
.h {font-family: Arial, Sans-serif;padding-top: 5px;padding-left: 10px;font-size  : 20px;font-weight: bold;color : #000000;}
.i {font-family: Arial, Sans-serif;padding-top: 5px;padding-left: 10px;font-size  : 14px;font-weight: bold;color : #000000;}
table {vertical-align: top;background-color: #f0f0f0;}
</style>
<body vlink='#000000' link='#000000' bgcolor='#6A71A3' leftmargin=0 topmargin=5 marginwidth="0" marginheight="0">
<table cellpadding="2" cellspacing="1" width="80%" align="center">
  <tr>
    <td>
      <table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
        <tr>
          <td height="30" valign="bottom" class="h">
           <span style="COLOR: #cc0000">&gt;&gt;</span> 欢迎安装本系统
          </td>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
        <tr>
          <td class="t">
           欢迎来到 欢迎安装本系统 安装向导，安装前请仔细阅读 licence 档的每个细节，在您确定可以完全满足 欢迎安装本系统 的授权协议之后才能开始安装。readme 档提供了有关软件安装的说明，请您同样仔细阅读，以保证安装进程的顺利进行。
          </td>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
        <tr>
         <td class='t' valign='top' colspan='2'>
          <b>注意:</b><br>
          <span class='r'>此种状况表示状态正确.</span><br>
          <span class='c'>此种状况表示状态发生错误.</span>
         </td>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
<?php

if (!$action) {
	$Happy_licence = file_get_contents('./licence.txt');
	$Happy_licence = str_replace('  ', '&nbsp; ', nl2br($Happy_licence));

	?>
        <tr>
         <td class='t' align=center><font color="#0000EE"><b>Happy 用户许可协议</b></font></td>
        </tr>
        <tr>
         <td class='t'><b><font color="#99ccff">&gt;</font><font color="#000000"> 请您务必仔细阅读下面的许可协议</font></b></td>
        </tr>
        <tr>
          <td class='t'><br>Happy 软件使用协议<br>
          <?php echo $Happy_licence?>
          </td>
        </tr>
        <tr>
          <td align="center">
            <br>
            <form method="get" action="<?php echo $PHP_SELF?>">
              <input type="hidden" name="action" value="config">
              <input type="submit" value="我同意" style="height: 25">&nbsp;
              <input type="button" name="exit" value="不同意" style="height: 25" onclick="javascript: window.close();">
            </form>
          </td>
        </tr>
<?php

} elseif ($action == 'config') {
	$exist_error = false;
	$write_error = false;
	$correct = '......<span class="r">OK</span>';
	$incorrect = ' <span class="c">777属性检测不通过</span>';
	$uncorrect = ' <span class="c">文件不存在请上传此文件</span>';
	$w_check = array('upload', 'cache', 'tpl_c');
	$count = count($w_check);
	for($i = 0; $i < $count; $i++) {
		if (!file_exists('../' . $w_check[$i])) {
			$w_check[$i] .= $uncorrect;
			$exist_error = true;
		} elseif (is_writable('../' . $w_check[$i])) {
			$w_check[$i] .= $correct;
		} else {
			$w_check[$i] .= $incorrect;
			$exist_error = true;
		}
	}

	?>
        <tr>
         <td class='i' colspan='2' align='left'>
          <span style='color:#CC0000'>&gt;</span> 检查必要目录和文件是否可写，如果发生错误，请更改文件/目录属性 777
         </td>
        </tr>
        <tr>
          <td colspan=2 align=left class='t'>
           <?php foreach($w_check as $tmp)echo $tmp . "<br>\n";
	?>
          </td>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
        <tr>
         <td class='i' colspan='2'>
          <span style='color:#CC0000'>&gt;</span> 设置数据库资料与系统根目录
         </td>
        </tr>
<?php
	if (!$exist_error && !$write_error) {

		?>
        <tr>
        <form method="post" action="<?php echo $PHP_SELF?>">
         <td align=center><br>
         <table width="70%" cellspacing=0 cellpadding=1 align=center>
          <tr><td bgcolor='#6A71A3'>
           <table width="100%" cellspacing=1 cellpadding=3 align=center>
            <tr><td class='t' colspan=2 bgcolor='#6A71A3'><font color="#FFFFFF">服务器配置</font></td></tr>
            <tr>
             <td class='t' width='40%'>&nbsp;&nbsp;数据库服务器地址</td>
             <td class='t'><input type='text' name='host' value='127.0.0.1'></td>
            </tr>
            <tr>
             <td class='t'>&nbsp;&nbsp;数据库服务器类型</td>
             <td class='t'>
              <select name='type'>
               <option value='mysql' selected>mysql</option>
              </select>
             </td>
            </tr>
            <tr>
             <td class='t'>&nbsp;&nbsp;数据库用户名</td>
             <td class='t'><input type='text' name='db_user' value='root'></td>
            </tr>
            <tr>
             <td class='t'>&nbsp;&nbsp;数据库密码</td>
             <td class='t'><input type='text' name='password' value=''></td>
            </tr>
            <tr>
             <td class='t'>&nbsp;&nbsp;数据库名</td>
             <td class='t'><input type='text' name='db_database' value='happy'></td>
            </tr>
            <tr>
             <td class='t'>&nbsp;&nbsp;表区分符号(非必要.<b>请保持默认</b>)</td>
             <td class='t'><input type='text' name='tablepre' value='core_'></td>
            </tr>
           </table>
          </td></tr>
         </table>
        </td>
        </tr>
        <tr>
         <td align="center">
          <br>
          <input type="hidden" name="action" value="environment">
          <input type="hidden" name="saveconfig" value="1">
          <input type="submit" name="submit" value="保存配置信息" style="height: 25">
          <input type="button" name="exit" value="退出安装向导" style="height: 25" onclick="javascript: window.close();">
          <br><br>
         </td>
        </form>
        </tr>
<?php
	} else {

		?>
        <tr>
          <td align="center">
            <br>
            <form method="get" action="<?php echo $PHP_SELF?>">
              <input type="hidden" name="action" value="config">
              <input type="submit" name="submit" value="重新检查设置" style="height: 25">
              <input type="button" name="exit" value="退出安装向导" style="height: 25" onclick="javascript: window.close();">
            </form>
          </td>
        </tr>
<?php
	}
} elseif ($action == 'environment') {
	if ($_POST['saveconfig']) {
		$type = $_POST['type'];
		$host = $_POST['host'];
		$db_user = str_replace('$', '\\$', $_POST['db_user']);
		$password = str_replace('$', '\\$', $_POST['password']);
		$db_database = $_POST['db_database'];
		$tablepre = $_POST['tablepre'];
		$domain = $_SERVER['SERVER_NAME'];
		$HASHCODE = md5(time());
		$fp = fopen('./config.php', 'r');
		$configfile = fread($fp, filesize('./config.php'));
		fclose($fp);
		$configfile = str_replace("'type' => 'happy_type'", "'type' => '".$type."'", $configfile);
		$configfile = str_replace("'host' => 'happy_host'", "'host' => '".$host."'", $configfile);
		$configfile = str_replace("'db_user' => 'happy_db_user'", "'db_user' => '".$db_user."'", $configfile);
		$configfile = str_replace("'password' => 'happy_password'", "'password' => '".$password."'", $configfile);
		$configfile = str_replace("'db_database' => 'happy_db_database'", "'db_database' => '".$db_database."'", $configfile);
		$configfile = str_replace("'tablepre' => 'happy_tablepre'", "'tablepre' => '".$tablepre."'", $configfile);
		$configfile = str_replace("'domain' => 'happy_domain'", "'domain' => '".$domain."'", $configfile);
		$configfile = str_replace("'HASHCODE' => 'happy_HASHCODE'", "'HASHCODE' => '".$HASHCODE."'", $configfile);


		file_put_contents('../config.php', trim($configfile));
		include_once(dirname(__file__).'/../init.php');
		$sql = file_get_contents('./happy.sql');
		$sql = str_replace('happy_', $tablepre, $sql);
		$data = new Data();
		$data -> tableQuery($sql);
		file_put_contents('./install_lock.php', '1');
	}

	?>

<?php
	?>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
          <td><?php echo $installinfo?></td>
        </tr>
        <tr>
          <td>
            <hr align="center" width="99%" size="1" color=#9999cc>
          </td>
        </tr>
        <tr>
          <td align="center">
            <span class='c'><b>恭喜您，Happy 安装成功！</span><br>
            <span class='i'><a href="../index.php">点击这里进入Happy后台管理进一步设置</a></span><br><br>
          </td>
        </tr>
<?php
	}

?>
      </table>
    </td>
  </tr>
</table>
<br>
</body>
</html>